package com.android.svmc.app.mobile.controller;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;

import com.android.svmc.app.mobile.activity.R;
import com.android.svmc.app.mobile.activity.Splash;
import com.android.svmc.app.mobile.data.JsonData;
import com.android.svmc.app.mobile.util.CommonDefine;
import com.android.svmc.app.mobile.util.SettingUtils;


public class CheckNewRecordService extends Service {
	private ScheduledExecutorService scheduleTaskExecutor;
	private int total=0;
	private int count=0;
	private static final int NOTIFY_ME_ID=1337;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.i(CommonDefine.TAG, "oncreate service");
		SettingUtils.savebooleanPreferences(this, CommonDefine.IS_STARTED_SERVICE_NOTIFICATION, true);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i(CommonDefine.TAG, "onDestroy service");
		SettingUtils.savebooleanPreferences(this, CommonDefine.IS_STARTED_SERVICE_NOTIFICATION, false);
		if (scheduleTaskExecutor != null && !scheduleTaskExecutor.isShutdown()){
			scheduleTaskExecutor.shutdown();
		}
		count = 0;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		final SDCardController sdcard = new  SDCardController(CheckNewRecordService.this);
		scheduleTaskExecutor = Executors.newScheduledThreadPool(5);

		// This schedule a runnable task every 8 hours
		scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
		  public void run() {
		  //  doSomethingUseful();
			Log.i(CommonDefine.TAG, "onStartCommand service");		
			int temp = Integer.parseInt(sdcard.ReadFromSdCard(CommonDefine.APP_FOLDER+"/constant_picture.txt",""));
			Log.i(CommonDefine.TAG, ""+temp);
			if(WifiManagement.haveNetworkConnection()==true){
				new loadTotalPic().execute();
			}
			Log.i(CommonDefine.TAG, ""+total);
			if(total > temp){
				Log.i(CommonDefine.TAG, "co bai moi service");
				showNotification();
			}else {
				Log.i(CommonDefine.TAG, "khong co bai moi service");
				
				
			}
			  
		  }
		}, 0, 8, TimeUnit.HOURS);
			
	
		return START_STICKY;
	}
	public class loadTotalPic extends AsyncTask<Void, Void, Void>{
		
		@Override
		protected Void doInBackground(Void... arg0) {
			 JsonData json = new JsonData();
			 total = json.getTotalPictures();
			return null;
		}
		
		
	}

	 public void showNotification() {
		 NotificationManager notifyMgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
	    Notification notifyObj=new Notification(R.drawable.icon_app,
	                                        "Thông báo từ SEntertainment...",
	                                        System.currentTimeMillis());
	    PendingIntent i=PendingIntent.getActivity(this, 0,
	                            new Intent(this, Splash.class),
	                                              0);
	    
	    notifyObj.setLatestEventInfo(this, "Có truyện, ảnh mới trên máy chủ ",
	                            "nhấn vào đây để xem ", i);
	    notifyObj.number=++count;
//	    notifyObj.defaults |= Notification.DEFAULT_VIBRATE;
//	    notifyObj.defaults |= Notification.DEFAULT_SOUND;
	    notifyObj.flags|=Notification.FLAG_AUTO_CANCEL;    
	    notifyMgr.notify(NOTIFY_ME_ID, notifyObj);
	  }
	  
	

	
	

}
